import org.junit.Test;

import java.io.*;

public class ObjectStreamTest {
    public static void main(String[] args) {

    }

    @Test
    public void testObjectStream() throws IOException {
        File file = new  File("object.txt");
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
        oos.writeUTF("哈哈哈哈哈\n");
        oos.writeObject("火火恍恍惚惚\n");
        oos.flush();
        oos.close();
    }

    @Test
    public void testObjectStream2() throws IOException, ClassNotFoundException {
        File file = new File("object.txt");
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
        String str1 = ois.readUTF();
        System.out.println(str1);
        String str2 = (String) ois.readObject();
        System.out.println(str2);
    }
@Test
    public void testObjectStream3() throws IOException,ClassNotFoundException {
        File file = new File("object.dat");
        ObjectOutputStream oss = new ObjectOutputStream(new FileOutputStream(file));
        PersonTest p1 = new PersonTest("jack", 19);
        oss.writeObject(p1);
        oss.flush();
        oss.close();
    }
@Test
    public void testObjectStream4() throws IOException,ClassNotFoundException {
        File file = new File("object.dat");
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
        Object p1 = ois.readObject();
        System.out.println(p1);
    }
}

class PersonTest implements Serializable {
    String name;
    int age;
    @Serial
    private final static long serialVersionUID = 12134333333333335L;

    public PersonTest(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "PersonTest{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
